package com.gxa.shop.servlet.account;

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.dto.AccountDto;
import com.gxa.shop.bean.vo.AccountVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.account.AccountService;
import com.gxa.shop.service.account.impl.AccountServiceImpl;
import com.gxa.shop.util.MyFormatUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

@WebServlet(name = "SelectAccount",value = "/account/select")
public class SelectAccount extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer page= MyFormatUtils.toInteger(req.getParameter("page"));
        Integer limit= MyFormatUtils.toInteger(req.getParameter("limit"));
        String name=req.getParameter("username");
        String phone=req.getParameter("userPhone");

        AccountDto accountDto=new AccountDto();
        accountDto.setPage(page);
        accountDto.setLimit(limit);
        accountDto.setName(name);
        accountDto.setPhone(phone);
        AccountService accountService=new AccountServiceImpl();
        Long total=accountService.totalAccount(accountDto);
        R r;
        List<AccountVo> accountVo = Collections.emptyList();
        if (total>0){
            accountVo= accountService.selectAccount(accountDto);

            System.out.println(accountVo);
        }
//        r=R.ok().addData("total",total).addData("list",accountVo);
        r=accountVo!=null?R.ok().addData("total",total).addData("list",accountVo):R.ok("没有该信息");
        String jsonResult= JSON.toJSONString(r);

        PrintWriter writer=resp.getWriter();
        writer.write(jsonResult);
        writer.flush();
        writer.close();

    }
}
